package cn.nnxy.mybatisplustest.controller;

import cn.nnxy.mybatisplustest.entity.DateDaliy;
import cn.nnxy.mybatisplustest.service.DateDaliyService;
import org.apache.ibatis.reflection.ArrayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;


/**
 * @author huangjianquan
 * @date 2022/9/30
 **/
@Controller
public class DatedaliyController {

    @Autowired
    private DateDaliyService userService;


    /**
     * 动态田间P1p2
     *
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public String initOneyearDate() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 开始日期设为2024年1月1日
        LocalDate startDate = LocalDate.of(2024, 1, 1);
        // 结束日期设为2024年12月31日
        LocalDate endDate = LocalDate.of(2024, 12, 31);
        List<String> jiejiari = Arrays.asList("2024-01-01", "2024-02-10", "2024-02-11", "2024-02-12"
                , "2024-02-13", "2024-02-14", "2024-02-15", "2024-02-16", "2024-02-17", "2024-04-04", "2024-04-05", "2024-04-05"
                , "2024-05-01", "2024-05-02", "2024-05-03", "2024-05-04", "2024-05-05", "2024-06-08", "2024-06-09", "2024-06-10", "2024-09-15",
                "2024-09-16", "2024-09-17", "2024-10-01", "2024-10-02", "2024-10-03", "2024-10-04", "2024-10-05", "2024-10-06", "2024-10-07");

        List<String> zhoumo_buxiu = Arrays.asList("2024-02-04", "2024-02-18", "2024-04-07", "2024-04-28"
                , "2024-05-11", "2024-09-14", "2024-09-29", "2024-10-12");
        int p1 = 3;
        int p2 = 7;
        int p3 = 12;

        List<String> workday = new ArrayList<>();
        List<String> outWorkday = new ArrayList<>();
        // 循环遍历整年
        for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
            if (isworkday_in2024(date.format(formatter))) {
                workday.add(date.format(formatter));
            } else {
                outWorkday.add(date.format(formatter));
            }
            System.out.println(date.format(formatter));
        }
        workday.add("2025-01-02");
        workday.add("2025-01-03");
        workday.add("2025-01-04");
        workday.add("2025-01-05");
        workday.add("2025-01-06");
        workday.add("2025-01-07");
        workday.add("2025-01-08");
        workday.add("2025-01-09");
        workday.add("2025-01-10");
        workday.add("2025-01-11");
        workday.add("2025-01-12");
        workday.add("2025-01-13");
        workday.add("2025-01-14");
        List<DateDaliy> dateDaliys = new ArrayList<>();
        for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
            DateDaliy dateDaliy = new DateDaliy();
            String datestr = date.format(formatter);
            dateDaliy.setId(datestr);
            dateDaliy.setCurrentdate(conventTodate(datestr));
            int index = workday.indexOf(datestr);
            dateDaliy.setP1( conventTodate(workday.get(index + p1)));
            dateDaliy.setP2(conventTodate(workday.get(index + p2)));
            dateDaliy.setP3(conventTodate(workday.get(index + p3)));
            if (workday.contains(datestr)) {
                dateDaliy.setWorkday("1");
            } else {
                dateDaliy.setWorkday("2");
            }
            dateDaliys.add(dateDaliy);
        }
        userService.saveOrUpdateBatch(dateDaliys);

        return "添加成功";
    }



    @RequestMapping("/insert2023")
    @ResponseBody
    public String initOneyearDate2023() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 开始日期设为2024年1月1日
        LocalDate startDate = LocalDate.of(2023, 1, 1);
        // 结束日期设为2024年12月31日
        LocalDate endDate = LocalDate.of(2023, 12, 31);
        List<DateDaliy> dateDaliys = new ArrayList<>();
        for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
            DateDaliy dateDaliy = new DateDaliy();
            String datestr = date.format(formatter);
            dateDaliy.setId(datestr);
            dateDaliy.setCurrentdate(conventTodate(datestr));
            dateDaliy.setP1(conventTodate("2024-02-22"));
            dateDaliy.setP2(conventTodate("2024-02-29"));
            dateDaliy.setP3(conventTodate("2024-03-08"));
            dateDaliy.setWorkday("1");
            dateDaliys.add(dateDaliy);
        }
        userService.saveOrUpdateBatch(dateDaliys);

        return "添加成功";
    }

    public boolean isworkday_in2024(String str) {

        List<String> jiejiari = Arrays.asList("2024-01-01", "2024-02-10", "2024-02-11", "2024-02-12"
                , "2024-02-13", "2024-02-14", "2024-02-15", "2024-02-16", "2024-02-17", "2024-04-04", "2024-04-05", "2024-04-05"
                , "2024-05-01", "2024-05-02", "2024-05-03", "2024-05-04", "2024-05-05", "2024-06-08", "2024-06-09", "2024-06-10", "2024-09-15",
                "2024-09-16", "2024-09-17", "2024-10-01", "2024-10-02", "2024-10-03", "2024-10-04", "2024-10-05", "2024-10-06", "2024-10-07");

        List<String> zhoumo_buxiu = Arrays.asList("2024-02-04", "2024-02-18", "2024-04-07", "2024-04-28"
                , "2024-05-11", "2024-09-14", "2024-09-29", "2024-10-12");
        if (jiejiari.contains(str)) {
            return false;
        } else {
            Date date = conventTodate(str);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY) {
                if (zhoumo_buxiu.contains(str)) {
                    return true;
                } else {
                    return false;
                }

            } else {

                return true;

            }


        }


    }

    public Date conventTodate(String str){
        DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return simpleDateFormat.parse(str);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }


}
